<script>
	$(function () {

		layui.extend({
			dtree: '{/}/static/plugins/layui/extends/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
		}).use(['dtree','layer','jquery'], function(){
			var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
			
			// 初始化树
			var DemoTree = dtree.render({
				elem: "#id_tree",
				url: '/api/inter/tsMenuInfo/dtree',
				initLevel: "1",
				toolbar:true,
				toolbarWay:"follow",
				toolbarShow:[],
				width:'100%',
				menubar:true,
				menubarTips:{
					group:["refresh", {
						menubarId: "add_one",  // 按钮的唯一ID
						icon: "dtree-icon-roundadd", // 从dtreefont库中选取
						title: "新增目录",  // 按钮的提示标题
						handler: function(node, $div){  //扩展按钮的回调函数
							layer.open({
								type: 2,
								title: "新增目录",
								area: ['95%', '90%'],
								content: '/web/sys/tsMenuInfo/toAddOne'
							});
						}
					}, {
						menubarId: "adjust_sort",  // 按钮的唯一ID
						icon: "dtree-icon-sort", // 从dtreefont库中选取
						title: "调整目录顺序",  // 按钮的提示标题
						handler: function(node, $div){  //扩展按钮的回调函数
							layer.open({
								type: 2,
								title: "调整菜单顺序",
								area: ['95%', '90%'],
								content: '/web/sys/tsMenuInfo/toResort?pguid=0'
							});
						}
					}]
				},
				toolbarExt: [{
					toolbarId: "addMenuOne",
					icon: "dtree-icon-add-circle",
					title: "新增菜单",
					handler: function (node, $div) {
						var content, title, url;
						if (node.level == 1) {
							layer.open({
								type: 2,
								title: "新增菜单",
								area: ['95%', '90%'],
								content: '/web/sys/tsMenuInfo/toAddTwo?pguid=' + node.nodeId
							});
						} else if (node.level == 2) {
							layer.msg('二级菜单不能新增子菜单');
							return false
						} else if (node.level == 3) {
							return false
						}
					}
				},{
					toolbarId: "resort",
					icon: "dtree-icon-sort",
					title: "调整菜单顺序",
					handler: function (node, $div) {
						var content, title, url;
						if (node.level == 1) {
							layer.open({
								type: 2,
								title: "调整菜单顺序",
								area: ['95%', '90%'],
								content: '/web/sys/tsMenuInfo/toResort?pguid=' + node.nodeId
							});
						} else {
							layer.msg('子菜单无需调整');
							return false
						}
					}
				}]
			});
			
			// 绑定节点点击
			dtree.on("node('id_tree')" ,function(obj){
				var objd = obj.param;
				if (objd.level == 1 || objd.level == 2) {
					
					if(!obj.param.leaf){
						var $div = obj.dom;
						DemoTree.clickSpread($div);  //调用内置函数展开节点
					}
					
					layui.table.reload("main_table",{
						page: {
							curr: 1 //重新从第 1 页开始
						},
						where: {
							guid: objd.nodeId
						}
					})
					return;
				} else if (objd.level == 3) {
					layer.msg('三级菜单无子数据');
					return;
				}
			});
		});
	});
</script>